<template>
  <el-dropdown trigger="click" size="small" placement="bottom-end" @command="handleCommand">
    <div class="avatar-box">
      <el-avatar :size="20">{{ userStore.user?.user_name?.charAt(0) || '' }}</el-avatar>
      <span class="name">
        {{ userStore.user.user_name }}
        <!-- <el-icon class="el-icon--right"><arrow-down /></el-icon> -->
      </span>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="logout">退出登录</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { getCurrentInstance } from 'vue'
import Storage from '@/utils/storage'
import { useUserStore } from '@/store/user'
import { logout } from '@/api/login'
const { proxy } = getCurrentInstance()
const userStore = useUserStore()
const handleCommand = ( val ) => {
  if ( 'logout' == val ) {
    logout({ accessToken: userStore.token }).then(( res ) => {
      Storage.remove( 'token' )
      Storage.remove( 'user' )
      clearTimeout( Storage.get( 'TIME_OUT' ))
      Storage.remove( 'TIME_OUT' )
      proxy.$router.push( '/login' )
    })
  }
}
</script>

<style lang="scss" scoped>
.avatar-box {
  color: #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  cursor: pointer;
  .el-avatar {
    font-size: 12px;
  }
  .name {
    margin-left: 5px;
    // min-width: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
